﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ClientApplication
{
    public partial class Config : Form
    {
        //gisServer信息
        private GisServer gisServer = new GisServer();
        private string gisServerUrl = null;

        public string GisServerUrl
        {
            get { return gisServerUrl; }
            set { gisServerUrl = value; }
        }
        private string gisServerUsername = null;

        public string GisServerUsername
        {
            get { return gisServerUsername; }
            set { gisServerUsername = value; }
        }
        private string gisServerPassword = null;

        public string GisServerPassword
        {
            get { return gisServerPassword; }
            set { gisServerPassword = value; }
        }
        //database Server 信息
        private DBServer dbServer = new DBServer();
        private string dbServerUrl = null;

        public string DbServerUrl
        {
            get { return dbServerUrl; }
            set { dbServerUrl = value; }
        }
        private string dbServerUsername = null;

        public string DbServerUsername
        {
            get { return dbServerUsername; }
            set { dbServerUsername = value; }
        }
        private string dbServerPassword = null;

        public string DbServerPassword
        {
            get { return dbServerPassword; }
            set { dbServerPassword = value; }
        }
        public Config()
        {
            InitializeComponent();
            //button_ok.DialogResult = DialogResult.OK;
        }
        public Config(GisServer gis, DBServer db) 
        {
            InitializeComponent();

            this.gisServer = gis;

            this.dbServer = db;
        }
        private void writeAppConfig(GisServer gis, DBServer db) 
        {
            //获取Configuration对象
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            try
            {
                //写入<add>元素的Value
                //write gisServer info
                config.AppSettings.Settings["gisusername"].Value = gis.GisUsername;
                config.AppSettings.Settings["gispassword"].Value = gis.GisPassword;
                config.AppSettings.Settings["gisserver"].Value = gis.GisUrl;
                //write dbserver info
                config.AppSettings.Settings["dbusername"].Value = db.DbUsername;
                config.AppSettings.Settings["dbpassword"].Value = db.DbPassword;
                config.AppSettings.Settings["dbserver"].Value = db.DbUrl;
                config.AppSettings.Settings["dbname"].Value = db.DbName;
                //一定要记得保存，写不带参数的config.Save()也可以
                config.Save(ConfigurationSaveMode.Modified);
                //刷新，否则程序读取的还是之前的值（可能已装入内存）
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {


                throw new SystemException(ex.Message);
            }
        }
        private void writeAppConfig(string url, string username, string password)
        {
            ////获取Configuration对象
            // config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ////根据Key读取<add>元素的Value
            //string name = config.AppSettings.Settings["name"].Value;
            ////写入<add>元素的Value
            //config.AppSettings.Settings["name"].Value = "xieyc";
            ////增加<add>元素
            //config.AppSettings.Settings.Add("url", "http://www.xieyc.com");
            ////删除<add>元素
            //config.AppSettings.Settings.Remove("name");
            ////一定要记得保存，写不带参数的config.Save()也可以
            //config.Save(ConfigurationSaveMode.Modified);
            ////刷新，否则程序读取的还是之前的值（可能已装入内存）
            //System.Configuration.ConfigurationManager.RefreshSection("appSettings");



            //获取Configuration对象
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            try
            {
                //写入<add>元素的Value
                config.AppSettings.Settings["server"].Value = url;
                config.AppSettings.Settings["username"].Value = username;
                config.AppSettings.Settings["password"].Value = password;
                //一定要记得保存，写不带参数的config.Save()也可以
                config.Save(ConfigurationSaveMode.Modified);
                //刷新，否则程序读取的还是之前的值（可能已装入内存）
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            }
            catch ( Exception ex)
            {

               
                throw new SystemException(ex.Message);
            }
 
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(textBox_gisserverusername.Text) 
                || String.IsNullOrWhiteSpace(textBox_gisserverpassword.Text) 
                || String.IsNullOrWhiteSpace(textBox_gisserverurl.Text) || String.IsNullOrWhiteSpace(textBox_databaseName.Text)
                || String.IsNullOrWhiteSpace(textBox_databaseserverpassword.Text) || String.IsNullOrWhiteSpace(textBox_databasservereusername.Text) 
                || String.IsNullOrWhiteSpace(textBox_gisserverurl.Text))
            {
                MessageBox.Show("请输入完整的GISServer信息！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //writeAppConfig(textBox_gisserverurl.Text, textBox_gisserverusername.Text, textBox_gisserverpassword.Text);
                //this.gisServer = new GisServer();
                gisServer.GisUrl = textBox_gisserverurl.Text;
                gisServer.GisUsername = textBox_gisserverusername.Text;
                gisServer.GisPassword = textBox_gisserverpassword.Text;
                //
                //this.dbServer = new DBServer();
                dbServer.DbName = textBox_databaseName.Text;
                dbServer.DbUrl = textBox_databaseserverurl.Text;
                dbServer.DbUsername = textBox_databasservereusername.Text;
                dbServer.DbPassword = textBox_databaseserverpassword.Text;

                //

                this.writeAppConfig(this.gisServer, this.dbServer);
                this.Close();
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(this.textBox_gisserverurl.Text) && string.IsNullOrEmpty(this.textBox_gisserverusername.Text) && string.IsNullOrEmpty(this.textBox_gisserverpassword.Text))
            if (this.gisServer.IsNull() && this.dbServer.IsNull())
            {
                MessageBox.Show("点击OK取消设置，请在主界面配置中重新设置", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {

            }
        }

        private void Config_Load(object sender, EventArgs e)
        {
            this.button_ok.DialogResult = DialogResult.OK;

            this.button_cancle.DialogResult = DialogResult.Cancel;

            //display gisServer Info
            this.textBox_gisserverpassword.Text = this.gisServer.GisPassword;
            this.textBox_gisserverurl.Text = this.gisServer.GisUrl;
            this.textBox_gisserverusername.Text = this.gisServer.GisUsername;

            //display dbServer info
            this.textBox_databaseName.Text = this.dbServer.DbName;
            this.textBox_databaseserverpassword.Text = this.dbServer.DbPassword;
            this.textBox_databaseserverurl.Text = this.dbServer.DbUrl;
            this.textBox_databasservereusername.Text = this.dbServer.DbUsername;
        }


    }
}
